class Solution {
    boolean felf = false;
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        //判空和0， 数组越界
        if(matrix == null) {
            return felf;
        } 
        if(matrix.length == 0 ) {
            return false;
        }
        if(matrix[0].length == 0) {
            return false;
        }
        aa(matrix,0,target);
        return felf;
    }

    public void aa(int[][] matrix,int i,int target) {
        int gao = matrix.length;
        int kuan = matrix[0].length;
        if(felf == true) {
            return; 
        }
        //防止i加过头，数组越界
        if(i >= gao) {
            return;
        }
        //如果 target大，i++，递归，相等为true，return
        if(target > matrix[i][0]) {
            aa(matrix,i+1,target);
        } else if(target == matrix[i][0]){
            felf = true;
            return;
        }
        //防止target大于整个数组，而使得宽的多次遍历
        if(target > matrix[i][kuan-1]) {
            return;
        }
        for(int j = 0;j < kuan;j++) {
            if(matrix[i][j] == target) {
                felf = true;
                break;
            }
            if(matrix[i][j] > target) {
                break;
            }
        }
    }
}